/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ke.me.muhia.mvn.app.ucip;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;


import org.apache.http.message.BasicHeader;


import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.apache.xmlrpc.common.XmlRpcHttpRequestConfig;
import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Kenneth
 */
public class UCIPConnector {

    private static final Logger logger = LoggerFactory.getLogger(UCIPConnector.class);

    UCIPParams params = null;

    public UCIPConnector() {

    }

    public UCIPConnector(UCIPParams params) {
        this.params = params;
    }

    void getSubscriber(String subno) {
        try {
            String connUrl = "http://" + params.getIp() + ":" + params.getPort() + "/Air";
            XmlRpcClient ucipClient = new XmlRpcClient();
            
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            XmlRpcHttpRequestConfig httpRequestConfig = new XmlRpcHttpRequestConfigImpl();
            
            List<Header> headers = new ArrayList<>();
            
            Header header = new BasicHeader("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode(params.getAuthtokens().getBytes()));
            headers.add(header);
            header = new BasicHeader("Host",params.getHostname());
            headers.add(header);
            header = new BasicHeader("Content-Type", "text/xml");
            headers.add(header);
            header = new BasicHeader("User-Agent", params.getUserAgent());
            headers.add(header);
            HttpClient httpClient = HttpClients.custom().setDefaultHeaders(headers).build();
            
            
            
            XmlRpcCommonsTransportFactory xmlRpcCommonsTransportFactory = new XmlRpcCommonsTransportFactory(ucipClient);
            
            
//            ucipClient.setTransportFactory(new XmlRpcCommonsTransportFactory(ucipClient));
//            ucipClient.
            config.setServerURL(new URL(connUrl));
            config.setConnectionTimeout(params.getConnectTimeout());
            config.setReplyTimeout(params.getReadTimeout());
            config.setUserAgent(params.getUserAgent());
            config.setTimeZone(params.getTz());
            config.setEnabledForExtensions(true);
            config.setContentLengthOptional(true);
            config.setEnabledForExceptions(true);
            
            
            ucipClient.setConfig(config);
            xmlRpcCommonsTransportFactory.setHttpClient((org.apache.commons.httpclient.HttpClient) httpClient);
            ucipClient.setTransportFactory(xmlRpcCommonsTransportFactory);
            
            
            
            
        } catch (MalformedURLException ex) {
            logger.error("UCIPConnector.getSubscriber", ex);
        }

    }

}
